package com.chain.BrotherExchange.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ActivityOptionsCompat;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

import com.chain.BrotherExchange.C;
import com.chain.BrotherExchange.R;
import com.chain.BrotherExchange.data.model.UserInfo;
import com.chain.BrotherExchange.event.LoginEvent;
import com.chain.BrotherExchange.ui.core.BaseActivity;
import com.chain.BrotherExchange.ui.listener.OnSpecificEditorActionListener;
import com.chain.BrotherExchange.utils.ActivityBarHelper;
import com.chain.BrotherExchange.utils.Androids;
import com.chain.BrotherExchange.viewmodel.LoginViewModel;
import com.chain.BrotherExchange.viewmodel.core.ViewModelProviders;

import org.greenrobot.eventbus.EventBus;

import butterknife.Bind;

import static com.chain.BrotherExchange.C.EXTRA_MOBILE;

public class LoginActivity extends BaseActivity {

    @Bind(R.id.check_box) CheckBox mCheckBox;
    @Bind(R.id.login_button) Button mLoginButton;
    @Bind(R.id.register_view) View mRegisterView;
    @Bind(R.id.password_edit) EditText mPasswordEdit;
    @Bind(R.id.account_name_edit) EditText mMobileNumberEdit;
    @Bind(R.id.forget_password_view) View mForgetPasswordView;

    private LoginViewModel mViewModel;

    public static void transitionLaunch(View view) {
        Activity activity = (Activity) view.getContext();
        ActivityCompat.startActivity(activity, new Intent(activity, LoginActivity.class),
                ActivityOptionsCompat.makeSceneTransitionAnimation(activity, view,
                        activity.getString(R.string.transition_name_login_button)).toBundle());
    }

    public static void launch(Context context) {
        context.startActivity(new Intent(context, LoginActivity.class));
    }

    @Override
    protected void setStatusBar() {
        ActivityBarHelper.setTransparentStatus(this);
    }

    @Override
    protected int getLayoutID() {
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        return R.layout.activity_login;
    }

    @Override
    protected void initActions() {
        mViewModel = ViewModelProviders.of(this).get(LoginViewModel.class);

        // 改变密码可见性
        Androids.changePasswordVisibility(mCheckBox, mPasswordEdit);

        // 注册
        mRegisterView.setOnClickListener(v -> RegisterActivity.launch(mMobileNumberEdit));

        // 找回密码
        mForgetPasswordView.setOnClickListener(v -> ForgetPasswordActivity.launch(mMobileNumberEdit));

        // 登录
        mLoginButton.setOnClickListener(v -> login());
        mPasswordEdit.setOnEditorActionListener(new OnSpecificEditorActionListener(
                EditorInfo.IME_ACTION_DONE, (v, actionId, event) -> login()));
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == C.REQ_MOBILE_CODE && resultCode == RESULT_OK && data != null){
            mMobileNumberEdit.setText(data.getStringExtra(EXTRA_MOBILE));
        }
    }

    private boolean login() {
        String mobilePhone = mMobileNumberEdit.getText().toString();
        String password = mPasswordEdit.getText().toString();
        mViewModel.login(mobilePhone, password).observe(this, resource -> {
            switch (resource.state) {
                case LOADING:
                    Androids.showProgressMessage(this, R.string.on_login);
                    break;

                case ERROR:
                    Androids.dismissProgressMessage();
                    Androids.makeText(resource.message);
                    break;

                case SUCCESS:
                    Androids.dismissProgressMessage();
                    saveLoginDataOntoLocal(resource.source);
                    break;
            }
        });

        return true;
    }

    private void saveLoginDataOntoLocal(UserInfo.DataBeanX userLogin) {
        UserInfo.DataBeanX.DataBean loginData = userLogin.getDataX();
        EventBus.getDefault().post(new LoginEvent(loginData.getHead_scul(), loginData.getNick_name()));
        MainActivity.launch(this);
    }
}
